

/**
 * @file display.cpp
 * @brief human body health monitoring sensor
 * @author achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/04
 */

#include "display.h"

static String title = "Hello world, achdu0000!";

static lv_obj_t *arc;
static lv_obj_t *label;
static lv_point_t line_points[]={{100,100},{300,300}};
static lv_obj_t *line;
static lv_obj_t *btn;
static lv_timer_t *timer;
static lv_obj_t * my_Cir ;
static bool paused = false;
static uint32_t elapsed_time = 0;

static int my_Circle_r = 150; 

void set_r(int r)
{
    my_Circle_r = r;
}

static void update_clock(lv_timer_t *timer)
{
    if (!paused) {
        elapsed_time += timer->period;  // 累加定时器周期
    }
    
    uint32_t sec = elapsed_time / 1000;
    uint32_t milliseconds = elapsed_time % 1000;

    uint8_t h=sec/3600;
    sec%=3600;
    uint8_t m=sec/60;
    uint8_t s=sec%60;
    uint8_t ms=milliseconds/10;
    String hh = h<10? "0"+String(h): String(h);
    String mm = m<10? "0"+String(m): String(m);
    String ss = s<10? "0"+String(s): String(s);
    String msms = ms<10?"0"+String(ms):String(ms);
    

    String buf = hh+":"+mm+":"+ss+"."+ msms;
    LV_LOG_INFO(buf.c_str());
    lv_label_set_text(label, buf.c_str());

    // 计算arc的值，每1秒转一圈
    uint16_t arc_value = (milliseconds)*0.1;
    lv_arc_set_value(arc, arc_value);
}

static void btn_event_cb(lv_event_t *e)
{
    paused = !paused;  // 切换暂停状态
    lv_obj_t *my_label = lv_obj_get_child(lv_event_get_target(e),0);
    if (my_label) {
        lv_label_set_text(my_label, paused?"Resume":"Pause");
        set_r(paused?150:200);
        lv_obj_set_size(my_Cir,my_Circle_r*2, my_Circle_r*2);
    }
}

static void create_my_display()
{
    lv_obj_t *parent = lv_scr_act();

    // 创建一个 line 对象
    lv_obj_t *line = lv_line_create(parent);

        // 设置 line 对象的样式（例如颜色）
    static lv_style_t style_line;
    lv_style_init(&style_line);
    lv_style_set_line_width(&style_line, 2);
    lv_style_set_line_color(&style_line, lv_color_black());
    lv_style_set_line_rounded(&style_line, false);

    // 将样式应用到 line 对象
    lv_obj_add_style(line, &style_line, LV_PART_MAIN);

    // 设置 line 对象的点
    lv_line_set_points(line, line_points, 2);

    my_Cir = lv_obj_create(lv_scr_act());
    lv_obj_set_scrollbar_mode(my_Cir , LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_size(my_Cir , my_Circle_r*2, my_Circle_r*2);
    lv_obj_center(my_Cir);
    lv_obj_set_style_bg_opa(my_Cir, LV_OPA_TRANSP, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(my_Cir, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(my_Cir,10,LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(my_Cir, LV_RADIUS_CIRCLE, LV_PART_MAIN | LV_STATE_DEFAULT);

    // 创建arc对象
    arc = lv_arc_create(parent);
    lv_obj_center(arc);
    lv_obj_set_size(arc, 150, 150);
    lv_arc_set_bg_angles(arc, 0, 360);
    lv_arc_set_rotation(arc, 270);
    lv_arc_set_value(arc, 0);

    // 创建clock标签
    label = lv_label_create(parent);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);

    // 创建暂停按钮
    btn = lv_btn_create(parent);
    lv_obj_align(btn, LV_ALIGN_CENTER, 0, 100);
    lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *btn_label = lv_label_create(btn);
    lv_label_set_text(btn_label, "Pause");



    // 创建定时器，每50毫秒更新一次
    timer = lv_timer_create(update_clock, 50, NULL);

    
}

void log_cb(const char *buf)
{
    _LOG_SERIAL.println(buf);
}

void onCreate()
{
    lv_log_register_print_cb(log_cb);

    String log_msg = title + "starting";
    LV_LOG_INFO(log_msg.c_str());

    LV_LOG_INFO("Init panel device");

    ESP_Panel *panel = new ESP_Panel();
    panel->init();

#if LVGL_PORT_AVOID_TEAR
    // When avoid tearing function is enabled, configure the RGB bus according to the LVGL configuration
    ESP_PanelBus_RGB *rgb_bus = static_cast<ESP_PanelBus_RGB *>(panel->getLcd()->getBus());
    rgb_bus->configRgbFrameBufferNumber(LVGL_PORT_DISP_BUFFER_NUM);
    rgb_bus->configRgbBounceBufferSize(LVGL_PORT_RGB_BOUNCE_BUFFER_SIZE);
#endif

    panel->begin();

    LV_LOG_INFO("Init LVGL");
    lvgl_port_init(panel->getLcd(), panel->getTouch());

    LV_LOG_INFO("Create UI");
    /** Lock the mutex due to the LVGL APIs are not thread-safe */
    lvgl_port_lock(-1);

    create_my_display();

    /** Release the mutex */
    lvgl_port_unlock();

    log_msg = title + "ending";
    LV_LOG_INFO(log_msg.c_str());
}
